package com.changgou.seckill.mq;

import com.alibaba.fastjson.JSON;
import com.changgou.seckill.service.SeckillOrderService;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RabbitListener(queues = "${mq.pay.queue.seckillorder}")
public class SeckillMessageListener {

    @Autowired
    private SeckillOrderService seckillOrderService;

    /***
     * 消息监听
     * @param message
     */
    @RabbitHandler
    public void getMessage(String message) throws Exception {
        //获取回调信息
        Map<String, String> messageMap = JSON.parseObject(message,Map.class);
        //获取状态码 通信标识--》success
        String return_code = messageMap.get("return_code");
        //获取订单号
        String out_trade_no = messageMap.get("out_trade_no");
        //自定义数据
        String attach = messageMap.get("attach");
        Map<String,String> attachMap = JSON.parseObject(attach, Map.class);
        if (return_code.equals("SUCCESS")){
            //订单状态修改
            seckillOrderService.updatePayStatus(messageMap.get("transaction_id"),messageMap.get("time_end"),attachMap.get("username"));

        }else {
            //删除订单【真实工作中存入mysql】-》回滚库存
            seckillOrderService.closeOrder(attachMap.get("username"));
        }
    }
}
